package com.rocksdb;

import org.rocksdb.*;

public class RocksDBTransactionExample {
    static {
        RocksDB.loadLibrary();
    }

    private static final String txdbPath = "/opt/study/rocksdb";

    public static void main(String[] args) {
        try (final Options options = new Options().setCreateIfMissing(true);
             final OptimisticTransactionDB txnDb = OptimisticTransactionDB.open(options, txdbPath)) {
            byte[] key = "abc".getBytes();
            byte[] value = "def".getBytes();
            try (final WriteOptions writeOptions = new WriteOptions();
                 final ReadOptions readOptions = new ReadOptions()) {
                // 开始事务
                try (final Transaction txn = txnDb.beginTransaction(writeOptions)) {
                    // 在事务中写入数据
                    txn.put(key, value);
                    // 提交事务
                    txn.commit();
                }
            }

            System.out.println("value:"+new String(txnDb.get(key)));
        } catch (RocksDBException e) {
            e.printStackTrace();
        }
    }
}
